﻿#include "event_handler.h"

#include "log.h"
#include "world.h"  // for GetLuaState()
#include "get_world.h"  // for GetWorld()

const char LOG_NAME[] = "EventHandler";

EventHandler::EventHandler()
{
	assert(GetWorld());
	lua_State* L = GetWorld()->GetLuaState();
	assert(L);

	using LuaIntf::LuaRef;
	LuaRef require(L, "require");
	try
	{
		m_handler = require.call<LuaRef>("event_handler");
	}
	catch (const LuaIntf::LuaException& e)
	{
		LOG_WARN("Failed to require event_handler.lua. " << e.what());
	}
	if (m_handler.isTable())
		return;

	LOG_WARN("event_handler.lua should return a table.");
	m_handler = LuaRef();  // reset
}

EventHandler::~EventHandler()
{
}

